package com.ruoyi.web.controller.house;

import com.ruoyi.area.domain.HouseAreaExportT;
import com.ruoyi.area.domain.HouseAreaT;
import com.ruoyi.area.service.IHouseAreaTService;
import com.ruoyi.committee.domain.HouseCommitteeT;
import com.ruoyi.committee.service.IHouseCommitteeTService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.data.DataUtils;
import com.ruoyi.date.DateFormater;
import com.ruoyi.door.domain.HouseDoorT;
import com.ruoyi.door.service.IHouseDoorTService;
import com.ruoyi.group.domain.HouseGroupT;
import com.ruoyi.group.service.IHouseGroupTService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 小区管理Controller
 *
 * @author ruoyi
 * @date 2023-04-17
 */
@RestController
@RequestMapping("/house/area")
public class HouseAreaTController extends BaseController {
    @Autowired
    private IHouseAreaTService houseAreaTService;
    @Autowired
    private IHouseDoorTService houseDoorTService;
    @Autowired
    private IHouseGroupTService houseGroupTService;
    @Autowired
    private IHouseCommitteeTService houseCommitteeTService;

    /**
     * 查询小区管理列表
     */
    @GetMapping("/list1")
    public TableDataInfo list1(HouseAreaT houseAreaT) {
        startPage();
        List<HouseAreaExportT> list = houseAreaTService.selectHouseAreaTList(houseAreaT);
//        HouseAreaExportT houseArea = houseAreaTService.selectHouseAreaSum(houseAreaT);
//        list.add(houseArea);
        return getDataTable(list);
    }
    @GetMapping("/list2/{areaName}")
    public TableDataInfo list2(@PathVariable("areaName") String areaName) {
        startPage();
        HouseAreaT houseAreaT=new HouseAreaT();
        houseAreaT.setAreaName(areaName);
        List<HouseAreaExportT> list = houseAreaTService.selectHouseAreaTList(houseAreaT);
        return getDataTable(list);
    }

    /**
     * 投票小区列表  和物业公司关联的小区列表
     */
    @GetMapping("/list3")
    public TableDataInfo list3(HouseAreaT houseAreaT) {
        startPage();
        List<HouseAreaExportT> list = houseAreaTService.selectHouseAreaNPropertyList(houseAreaT);
        return getDataTable(list);
    }

    /**
     * 投票小区列表  和物业公司关联的小区列表  用作输入查找方法
     */
    @GetMapping("/list4/{areaName}")
    public TableDataInfo list4(@PathVariable("areaName") String areaName) {
        startPage();
        HouseAreaT houseAreaT=new HouseAreaT();
        houseAreaT.setAreaName(areaName);
        List<HouseAreaExportT> list = houseAreaTService.selectHouseAreaNPropertyList(houseAreaT);
        return getDataTable(list);
    }


    @GetMapping("/list")
    public TableDataInfo list(HouseAreaT houseAreaT) {
        startPage();
        HouseAreaExportT houseAreaExportT = new HouseAreaExportT();
        HouseAreaT houseAreaVo = new HouseAreaT();
        houseAreaVo.setAreaName(houseAreaT.getAreaName());
        houseAreaT.setLockSts(houseAreaT.getLockSts());
        List<HouseAreaExportT> list = houseAreaTService.selectHouseAreaTList(houseAreaT);
        for (int i = 0; i < list.size(); i++) {
            //开发企业信息
            houseAreaT = new HouseAreaT();
            houseAreaT.setAreaName(list.get(i).getAreaName());
            houseAreaT = houseAreaTService.getAreaEstateInfo(houseAreaT);
            if (houseAreaT != null) {
                list.get(i).setEstateName(houseAreaT.getEstateName());
                list.get(i).setEstater(houseAreaT.getEstater());
                list.get(i).setEstateTel(houseAreaT.getEstateTel());
            }
            //总户数-总面积
            HouseDoorT houseDoorVO = new HouseDoorT();
            houseDoorVO.setAreaName(list.get(i).getAreaName());
            houseDoorVO = houseDoorTService.getDoorNumber(houseDoorVO);
            if (houseDoorVO != null) {
                list.get(i).setDoorNumber(houseDoorVO.getDoorNumber());
                list.get(i).setSpaceNum(houseDoorVO.getSpaceNum());
            }else{
                list.get(i).setDoorNumber("0");
                list.get(i).setSpaceNum(0d);
            }

            //第一笔缴费日期
            HouseAreaT houseAreaVO = new HouseAreaT();
            houseAreaT.setAreaName(list.get(i).getAreaName());
            houseAreaVO = houseAreaTService.getCostDate(houseAreaT);
            if (houseAreaVO != null) {
                list.get(i).setCostDate(houseAreaVO.getCostDate());
                //缴费时长
                String nowDate = DateFormater.getSystemCurrentDateSimple();
                Double subDate = DataUtils.doubleScale(Double.valueOf(DateFormater.dateSubtractionStr(nowDate, houseAreaVO.getCostDate())) / 365);
                list.get(i).setDuration(subDate);
            }

            //缴费户数
            houseAreaT.setAreaName(list.get(i).getAreaName());
            houseAreaVO = houseAreaTService.getCostNum(houseAreaT);
            if (houseAreaVO != null) {
                list.get(i).setCostNum(houseAreaVO.getCostNum());
                //已缴户数比率
                Double costRatio = 0d;
                if(!("0".equals(houseDoorVO.getDoorNumber()))){
                    costRatio = DataUtils.doubleScale1(Double.valueOf(houseAreaVO.getCostNum()) / Double.valueOf(houseDoorVO.getDoorNumber())) * 100;
                }
                Integer costRatio1 = Integer.valueOf(costRatio.intValue());
                list.get(i).setCostRatio(String.valueOf(costRatio1) + "%");
            }else{
                list.get(i).setCostNum("0");
                //已缴户数比率
                Double costRatio = 0d;
                Integer costRatio1 = Integer.valueOf(costRatio.intValue());
                list.get(i).setCostRatio(String.valueOf(costRatio1) + "%");
            }

            //已徼/未缴
            houseAreaT.setAreaName(list.get(i).getAreaName());
            houseAreaVO = houseAreaTService.getAreaCost(houseAreaT);
            if (houseAreaVO != null) {
                list.get(i).setCostMoney(houseAreaVO.getCostMoney());
                list.get(i).setBudgetMoney(houseAreaVO.getBudgetMoney());
                //未缴-已徼=差额
                list.get(i).setCostMargin(DataUtils.costMargin(houseAreaVO.getBudgetMoney(), houseAreaVO.getCostMoney()));
            }
        }
        if(getDataTableSumJudge(list)){
            HouseDoorT houseDoorVO = new HouseDoorT();
            houseDoorVO.setAreaName(houseAreaVo.getAreaName());
            houseDoorVO.setLockSts(houseAreaVo.getLockSts());
            houseDoorVO = houseDoorTService.getDoorNumber1(houseDoorVO);
            houseAreaExportT.setDoorNumber(houseDoorVO.getDoorNumber());
            houseAreaExportT.setSpaceNum(houseDoorVO.getSpaceNum());
            HouseAreaT houseAreaVO = new HouseAreaT();
            houseAreaVO = houseAreaTService.getCostNum1(houseAreaVo);
            houseAreaExportT.setCostNum(houseAreaVO.getCostNum());
            houseAreaVO = houseAreaTService.getAreaCost1(houseAreaVo);
            houseAreaExportT.setCostMoney(houseAreaVO.getCostMoney());
            houseAreaExportT.setBudgetMoney(houseAreaVO.getBudgetMoney());
            //未缴-已徼=差额
            houseAreaExportT.setCostMargin(DataUtils.costMargin(houseAreaVO.getBudgetMoney(), houseAreaVO.getCostMoney()));
            if(houseAreaExportT!=null){
                houseAreaExportT.setAreaName("合计");
            }
            list.add(houseAreaExportT);
        }
        return getDataTable(list);
    }

    /**
     * 导出小区管理列表
     */
   @Log(title = "小区管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, HouseAreaT houseAreaT) {
        HouseAreaExportT houseAreaExportT = new HouseAreaExportT();
        HouseAreaT houseAreaVo = new HouseAreaT();
        houseAreaVo.setAreaName(houseAreaT.getAreaName());
        houseAreaT.setLockSts(houseAreaT.getLockSts());
        List<HouseAreaExportT> list = houseAreaTService.selectHouseAreaTList(houseAreaT);
        for (int i = 0; i < list.size(); i++) {
            //开发企业信息
            houseAreaT = new HouseAreaT();
            houseAreaT.setAreaName(list.get(i).getAreaName());
            houseAreaT = houseAreaTService.getAreaEstateInfo(houseAreaT);
            if (houseAreaT != null) {
                list.get(i).setEstateName(houseAreaT.getEstateName());
                list.get(i).setEstater(houseAreaT.getEstater());
                list.get(i).setEstateTel(houseAreaT.getEstateTel());
            }
            //总户数-总面积
            HouseDoorT houseDoorVO = new HouseDoorT();
            houseDoorVO.setAreaName(list.get(i).getAreaName());
            houseDoorVO = houseDoorTService.getDoorNumber(houseDoorVO);
            if (houseDoorVO != null) {
                list.get(i).setDoorNumber(houseDoorVO.getDoorNumber());
                list.get(i).setSpaceNum(houseDoorVO.getSpaceNum());
            }else{
                list.get(i).setDoorNumber("0");
                list.get(i).setSpaceNum(0d);
            }

            //第一笔缴费日期
            HouseAreaT houseAreaVO = new HouseAreaT();
            houseAreaT.setAreaName(list.get(i).getAreaName());
            houseAreaVO = houseAreaTService.getCostDate(houseAreaT);
            if (houseAreaVO != null) {
                list.get(i).setCostDate(houseAreaVO.getCostDate());
                //缴费时长
                String nowDate = DateFormater.getSystemCurrentDateSimple();
                Double subDate = DataUtils.doubleScale(Double.valueOf(DateFormater.dateSubtractionStr(nowDate, houseAreaVO.getCostDate())) / 365);
                list.get(i).setDuration(subDate);
            }

            //缴费户数
            houseAreaT.setAreaName(list.get(i).getAreaName());
            houseAreaVO = houseAreaTService.getCostNum(houseAreaT);
            if (houseAreaVO != null) {
                list.get(i).setCostNum(houseAreaVO.getCostNum());
                //已缴户数比率
                Double costRatio = 0d;
                if(!("0".equals(houseDoorVO.getDoorNumber()))){
                    costRatio = DataUtils.doubleScale1(Double.valueOf(houseAreaVO.getCostNum()) / Double.valueOf(houseDoorVO.getDoorNumber())) * 100;
                }
                Integer costRatio1 = Integer.valueOf(costRatio.intValue());
                list.get(i).setCostRatio(String.valueOf(costRatio1) + "%");
            }else{
                list.get(i).setCostNum("0");
                //已缴户数比率
                Double costRatio = 0d;
                Integer costRatio1 = Integer.valueOf(costRatio.intValue());
                list.get(i).setCostRatio(String.valueOf(costRatio1) + "%");
            }

            //已徼/未缴
            houseAreaT.setAreaName(list.get(i).getAreaName());
            houseAreaVO = houseAreaTService.getAreaCost(houseAreaT);
            if (houseAreaVO != null) {
                list.get(i).setCostMoney(houseAreaVO.getCostMoney());
                list.get(i).setBudgetMoney(houseAreaVO.getBudgetMoney());
                //未缴-已徼=差额
                list.get(i).setCostMargin(DataUtils.costMargin(houseAreaVO.getBudgetMoney(), houseAreaVO.getCostMoney()));
            }
        }
        if(getDataTableSumJudge(list)){
            HouseDoorT houseDoorVO = new HouseDoorT();
            houseDoorVO.setAreaName(houseAreaVo.getAreaName());
            houseDoorVO.setLockSts(houseAreaVo.getLockSts());
            houseDoorVO = houseDoorTService.getDoorNumber1(houseDoorVO);
            houseAreaExportT.setDoorNumber(houseDoorVO.getDoorNumber());
            houseAreaExportT.setSpaceNum(houseDoorVO.getSpaceNum());
            HouseAreaT houseAreaVO = new HouseAreaT();
            houseAreaVO = houseAreaTService.getCostNum1(houseAreaVo);
            houseAreaExportT.setCostNum(houseAreaVO.getCostNum());
            houseAreaVO = houseAreaTService.getAreaCost1(houseAreaVo);
            houseAreaExportT.setCostMoney(houseAreaVO.getCostMoney());
            houseAreaExportT.setBudgetMoney(houseAreaVO.getBudgetMoney());
            //未缴-已徼=差额
            houseAreaExportT.setCostMargin(DataUtils.costMargin(houseAreaVO.getBudgetMoney(), houseAreaVO.getCostMoney()));
            if(houseAreaExportT!=null){
                houseAreaExportT.setAreaName("合计");
            }
            list.add(houseAreaExportT);
        }
        ExcelUtil<HouseAreaExportT> util = new ExcelUtil<HouseAreaExportT>(HouseAreaExportT.class);
        util.exportExcel(response, list, "小区管理数据");
    }

    /**
     * 获取小区管理详细信息
     */
    @GetMapping(value = "/{areaId}")
    public AjaxResult getInfo(@PathVariable("areaId") Long areaId) {
        return AjaxResult.success(houseAreaTService.selectHouseAreaTByAreaId(areaId));
    }

    /**
     * 新增小区管理
     */
    @Log(title = "小区管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HouseAreaT houseAreaT) {
        houseAreaT.setCreater(getUsername());
        return toAjax(houseAreaTService.insertHouseAreaT(houseAreaT));
    }

    /**
     * 修改小区管理
     */
    @Log(title = "小区管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HouseAreaT houseAreaT) {
        return toAjax(houseAreaTService.updateHouseAreaT(houseAreaT));
    }

    @Log(title = "小区迁移管理", businessType = BusinessType.UPDATE)
    @PutMapping(value = "/moveArea")
    public AjaxResult moveArea(@RequestBody HouseAreaT houseAreaT)
    {
        HouseGroupT houseGroupT = new HouseGroupT();
        houseGroupT.setAreaName(houseAreaT.getAreaNameTemp());
        List<HouseGroupT> houseGroupTList = houseGroupTService.selectHouseGroupTByGroupName(houseGroupT);
        for(HouseGroupT houseGroupT1:houseGroupTList){
            houseGroupT1.setAreaName(houseAreaT.getAreaName());
            houseGroupTService.moveHouseGroupT(houseGroupT1);
        }
        return toAjax(1);
    }

    /**
     * 删除小区管理
     */
    @Transactional
    @Log(title = "小区管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{areaIds}")
    public AjaxResult remove(@PathVariable Long[] areaIds) {
        HouseAreaT houseAreaVO = new HouseAreaT();
        HouseGroupT houseGroupVO = new HouseGroupT();
        for (Long areaId : areaIds) {
            houseAreaVO = houseAreaTService.selectHouseAreaTByAreaId(areaId);
            houseGroupVO.setAreaName(houseAreaVO.getAreaName());
            List<HouseGroupT> list = new ArrayList<>();
            list = houseGroupTService.selectHouseGroupTByGroupName(houseGroupVO);
            for (int i = 0; i < list.size(); i++) {
                if ((list.get(i).getGroupName() != null)) {
                    return AjaxResult.error("存在楼宇,不允许删除");
                }
            }
        }
        return toAjax(houseAreaTService.deleteHouseAreaTByAreaIds(areaIds));
    }

    /**
     * 获取社区
     */
    @GetMapping("/getCommitteeList")
    public TableDataInfo getCommitteeList(HouseAreaT houseAreaT) {
        List<HouseCommitteeT> list = houseAreaTService.getCommitteeList();
        return getDataTable(list);
    }
}
